package rs.fon.neuroph.classification;

import java.util.ArrayList;
import java.util.Iterator;

import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.learning.TrainingElement;

import com.rapidminer.example.Attribute;
import com.rapidminer.example.Example;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.learner.SimplePredictionModel;

public class NeurophClassificationModel extends SimplePredictionModel {

	private static final long serialVersionUID = -4946664108344695140L;

	NeuralNetwork nnet;
	NeurophClassificationAdapter adapter;

	public NeurophClassificationModel(ExampleSet headerExampleSet, NeuralNetwork nnet, NeurophClassificationAdapter adapter) {
		super(headerExampleSet);
		this.nnet = nnet;
		this.adapter = adapter;
	}

	@Override
	public double predict(Example example) throws OperatorException {

		ArrayList<Double> values = new ArrayList<Double>();
		for (Iterator<Attribute> i = example.getAttributes().iterator(); i.hasNext();)
			values.add(example.getNumericalValue(i.next()));

		nnet.setInput((new TrainingElement(values)).getInput());
		nnet.calculate();
		double[] networkOutput = nnet.getOutput();

		double maxOutput = 0;
		int index = 0;
		for (int i = 0; i < networkOutput.length; i++) {
			if (networkOutput[i] > maxOutput) {
				maxOutput = networkOutput[i];
				index = i;
			}
		}
		
		ArrayList<Double> outputVector = new ArrayList<Double>();
			
					
		for (double d : networkOutput) {
			outputVector.add((double)0);
		}
		outputVector.set(index, (double) 1);

		//vrednost labela, ne njegov index, ali je double, mozda ce da puca?
		double predictedLabelValue = (Double) adapter.getLabelIndexFromOutputVector(outputVector);

		return predictedLabelValue;
	}

}
